#let niit_blue = rgb(4, 85, 146)
#let niitginkgo = rgb(4, 85, 146)
#let niit_logo_path = "./pic/niitlogo.png"
#let niit_logo_circle = "./pic/niitcircle.png"
#let mainbuilding_path = "./pic/background_1.png"
#let page_break_flag = state("page_break_flag", true)

#let beamer_start(title: none, subtitle: none, author: none, date: none) = {
  page(
    margin: (top: 20%, bottom: 5%, left: 5%, right: 5%),
    background: image(mainbuilding_path, height: 100%),
    header: align(right, image(niit_logo_path, width: 25%)),
    numbering: none,
  )[
    #if title != none {
      align(center + horizon, text(48pt, weight: "bold", title, fill: niit_blue))
    }
    #if author != none {
      align(center + horizon, text(24pt, weight: "regular", author, fill: niit_blue))
    }
    #if date != none {
      align(left + bottom, text(15pt, date, fill: niit_blue))
    }
  ]
}


#let beamer_catalogs() = {
  set page(
    margin: (top: 0%, bottom: 0%, left: 0%, right: 10%),
    numbering: none,
  )
  set outline(
    title: none,
    depth: 2,
    indent: 2em,
  )
  set outline.entry(fill: none)
  show outline.entry.where(level: 1): it => {
    v(30pt, weak: true)
    text(fill: niit_blue.lighten(10%), size: 20pt, weight: "bold", it)
  }
  show outline.entry.where(level: 2): it => {
    text(fill: niit_blue.lighten(20%), size: 15pt, weight: "regular", it)
  }
  grid(
    columns: (35%, 70%),
    column-gutter: 16%,
    align(
      center + horizon,
      box(
        fill: niit_blue,
        width: 100%,
        height: 100%,
        text(fill: white, size: 40pt, hyphenate: true, weight: "bold", [汇报内容]),
      ),
    ),
    align(center + horizon, outline()),
  )
}

#let beamer_content(body) = {
  set page(margin: (top: 3%, bottom: 3%, left: 5%, right: 5%))

  show heading.where(level: 1): it => {
    set page(
      margin: (top: 5%, bottom: 5%, left: 5%, right: 5%),
      fill: niitginkgo,
      header: none,
      background: none,
      numbering: none,
    )
    align(center + horizon, text(fill: white, size: 40pt, it))
  }

  let level2-count = state("level2-count", -1)

  show heading.where(level: 2): it => {
    level2-count.update(n => n + 1)
    context {
      let count = level2-count.get()
      counter(page).update(count)

      let level_1_now_title = query(selector(heading.where(level: 1)).before(here())).last().body
      let level_2_now_title = query(selector(heading.where(level: 2)).before(here())).last().body

      let first_title = none
      let get = false
      for item in query(heading.where(level: 1).or(heading.where(level: 2))) {
        if get {
          if item.level == 2 {
            first_title = item.body
          }
          break
        } else {
          if item.level == 1 and item.body == level_1_now_title {
            get = true
          }
        }
      }

      if first_title != level_2_now_title {
        pagebreak()
      }
    }

    grid(
      columns: (18fr, 1fr),
      gutter: 0%,
      align(left + horizon, text(fill: niit_blue, size: 20pt, weight: "bold", it)),
      align(right + horizon, image(niit_logo_circle, width: 100%)),

      align(top, line(length: 100%, stroke: (paint: niit_blue, thickness: 1pt))),
    )
  }
  body
}

#let beamer_end() = {
  set page(fill: niit_blue, numbering: none)
  set align(center + horizon)

  text(40pt, weight: "bold", fill: white)[感谢聆听！\ 敬请批评指导！]
}

#let beamer(title: none, subtitle: none, author: none, date: datetime(year: 2023, month: 7, day: 15), body) = {
  set page(
    paper: "presentation-16-9",
    number-align: right,
  )
  set text(
    font: (
      (name: "Courier New", covers: "latin-in-cjk"),
      "SimHei",
    ),
    lang: "zh",
    size: 18pt,
    weight: "regular",
  )

  let colored-numbering(..nums) = {
    let page-num = nums.pos().first()
    place(
      right + bottom,
      dx: -0.45em,
      dy: -1.5em,
      circle(
        radius: 0.6em,
        fill: niitginkgo,
        stroke: none,
        align(center + horizon)[
          #text(
            fill: white,
            weight: "bold",
            size: 14pt,
          )[#page-num]
        ],
      ),
    )
  }
  set page(numbering: colored-numbering)

  beamer_start(title: title, author: author, date: date)
  beamer_catalogs()
  beamer_content(body)
  beamer_end()
}
